<?php

/**
 * buildings controller for buildings management
 * 
 * @author HungPV <phamvanhung0818@gmail.com>
 * @package admin
 * @since 1.0
 */
class buildings extends MX_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('category/category');
        $this->load->model('building/building');
        $this->load->model('buildingimages/buildingimage');
    }

    function show($row = 0) {
        if($this->session->userdata('login')!=1)
        {
            redirect('admin/login');
        }
        //paging
        include('paging.php');
        $config['per_page'] = 10;
        $config['base_url'] = base_url() . 'admin/buildings/show/';

        $config['total_rows'] = $this->building->get_count();
        $config['cur_page'] = $row;
        $this->pagination->initialize($config);
        $data['list_link'] = $this->pagination->create_links();

        $buildings = $this->building->get_buildings($row, $config['per_page']);
        

        $data['buildings'] = $buildings;
        $data['view'] = 'buildings_show';
        $this->load->view('back_end/template', $data);
    }

    function add() {
        if($this->session->userdata('login')!=1)
        {
            redirect('admin/login');
        }
        $flag = TRUE;
        $hide = TRUE;

        if ($this->input->post('name')) {
            $hide = FALSE;

            $name = $this->input->post('name');
            $description = $this->input->post('description');
            $cat = $this->input->post('category');
            $images = $this->input->post('images');

            $id = $this->building->add($name, $description, $cat);
            if ($id > 0) {
                $flag = TRUE;
                if (strlen($images) > 0) {
                    $image_arr = explode("||", $images);
                    $count = count($image_arr);
                    for ($i = 0; $i < $count - 1; $i++) {
                        $this->buildingimage->add($id, 'files/' . $image_arr[$i], $image_arr[$i]);
                    }
                }
            } else {
                $flag = FALSE;
            }
        }

        $categories = $this->category->get_categories(-1);

        $data['categories'] = $categories;
        $data['flag'] = $flag;
        $data['hide'] = $hide;
        $data['view'] = 'buildings_add';
        $this->load->view('back_end/template', $data);
    }

    function edit($id) {
        if($this->session->userdata('login')!=1)
        {
            redirect('admin/login');
        }
        if ($this->input->post('name')) {

            $building_id = $this->input->post('building_id');
            $name = $this->input->post('name');
            $description = $this->input->post('description');
            $cat = $this->input->post('category');
            $images = $this->input->post('images');

            $this->building->edit($building_id, $name, $description, $cat);            

            if (strlen($images) > 0) {
                $image_arr = explode("||", $images);
                $count = count($image_arr);
                for ($i = 0; $i < $count - 1; $i++) {
                    $this->buildingimage->add($building_id, 'files/' . $image_arr[$i], $image_arr[$i]);
                }
            }
        }

        $categories = $this->category->get_categories(-1);
        $building = $this->building->get($id);
        
        
        
        $images_arr = $this->buildingimage->get_building_images($id);
    
        ////////////////////////////
        ///// check image path /////
        ////////////////////////////
        //echo file_exists($images_arr[0]->image_path);
        
        $data['images'] = $images_arr;
        $data['building'] = $building;
        $data['categories'] = $categories;
        $data['view'] = 'buildings_edit';
        $this->load->view('back_end/template', $data);
    }

    function delete($id) {
        $this->building->delete($id);
    }

    function deleteimage($id) {
        $this->buildingimage->delete($id);
    }

}

?>
